package com.xdjd.task.service;

import com.xdjd.task.constant.Constant;
import com.xdjd.task.model.activity.ActivityChallengeRecordDetailPo;
import com.xdjd.task.model.activity.ActivityChallengeRecordPo;
import com.xdjd.task.model.activity.ActivityClockDetailPo;
import com.xdjd.task.model.activity.ActivityUserClockPo;
import com.xdjd.task.model.member.MemberUserPo;
import com.xdjd.task.service.daoservice.*;
import com.xdjd.task.util.DateUtil;
import org.apache.commons.math3.random.RandomDataGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class InitDayStepService {

    @Autowired
    private MemberUserPoService memberUserPoService;
    @Autowired
    private MemberUserStepPoService memberUserStepPoService;
    @Autowired
    private ActivityChallengeRecordDetailPoService activityChallengeRecordDetailPoService;
    @Autowired
    private ActivityChallengeRecordPoService activityChallengeRecordPoService;
    @Autowired
    private ActivityUserClockPoService activityUserClockPoService;
    @Autowired
    private ActivityClockDetailPoService activityClockDetailPoService;


    public void initStep() {
        List<MemberUserPo> allUser = memberUserPoService.findAllUser();
        for (MemberUserPo userPo : allUser) {
            long stepNumber = new RandomDataGenerator().nextLong(1000, 20000);
            memberUserStepPoService.updateStepNumber(userPo.getId(), stepNumber);
            activityChallengeRecordDetailPoService.updateTodayStepByMemberId(userPo.getId(), stepNumber);
            if (stepNumber >= Constant.CHANCE_STEP.longValue()) {
                memberUserStepPoService.addChanceCountLock(userPo.getId());
                ActivityChallengeRecordPo recordPo = activityChallengeRecordPoService.findTodayRecordByMemberId(userPo.getId());
                if (recordPo == null) {
                    recordPo = new ActivityChallengeRecordPo();
                    recordPo.setMemberId(userPo.getId());
                    recordPo.setRecordNo("XD" + DateUtil.DateToString(new Date(), "yyyyMMdd") + userPo.getId());
                    recordPo.setChallengeCount(0);
                    recordPo.setStepNumber(stepNumber);
                    recordPo.setRecordDate(new Date());
                    activityChallengeRecordPoService.saveRecord(recordPo);
                    ActivityChallengeRecordDetailPo detailPo = new ActivityChallengeRecordDetailPo();
                    detailPo.setRecordNo(recordPo.getRecordNo());
                    detailPo.setMemberId(userPo.getId());
                    detailPo.setStepNumber(stepNumber);
                    detailPo.setRecordDate(new Date());
                    detailPo.setIsOwn(new Byte("1"));
                    activityChallengeRecordDetailPoService.saveRecordDetail(detailPo);
                } else {
                    activityChallengeRecordPoService.updateTodayStepByMemberId(userPo.getId(), stepNumber);
                }
            }
            ActivityUserClockPo userClockPo = activityUserClockPoService.findByMemberId(userPo.getId());
            if (userClockPo != null) {
                ActivityClockDetailPo clockDetailPo = activityClockDetailPoService.findByClockNoAndDate(userClockPo.getClockNo(), DateUtil.getDate(new Date()));
                if (clockDetailPo == null) {
                    if (userClockPo.getRemainDay().intValue() > 0) {
                        clockDetailPo = new ActivityClockDetailPo();
                        clockDetailPo.setMemberId(userPo.getId());
                        clockDetailPo.setClockNo(userClockPo.getClockNo());
                        clockDetailPo.setTargetStepNumber(userClockPo.getDayStepNumber());
                        clockDetailPo.setTodayStepNumber(stepNumber);
                        clockDetailPo.setStatus(new Byte("0"));
                        clockDetailPo.setCompleteStatus(new Byte("0"));
                        if (stepNumber >= userClockPo.getDayStepNumber().longValue()) {
                            clockDetailPo.setStatus(new Byte("1"));
                        }
                        clockDetailPo.setClockDate(new Date());
                        activityClockDetailPoService.saveClockDetail(clockDetailPo);
                        activityUserClockPoService.reduceRemainDay(userClockPo.getClockNo());
                    }
                } else {
                    clockDetailPo.setTodayStepNumber(stepNumber);
                    if (stepNumber >= clockDetailPo.getTargetStepNumber().longValue()) {
                        clockDetailPo.setStatus(new Byte("1"));
                    }
                    activityClockDetailPoService.updateClockDetail(clockDetailPo);
                }
            }
        }
    }

    public void initUserStep() {
        memberUserStepPoService.initUserStep();
    }
}
